package com.jy.framework.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.jy.framework.filter.CommonFieldMetaObjectHandler;
import com.jy.framework.utils.MapWrapperFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-plus配置
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.1.0 2018-02-05
 */
@Configuration
@MapperScan(basePackages = {"com.jy.web.*.dao","com.jy.framework.sys.dao"})
public class MybatisPlusConfig {
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 配置乐观锁
     *
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 公共字段填充
     *
     * @return
     */
    @Bean
    public CommonFieldMetaObjectHandler commonFieldMetaObjectHandler() {
        return new CommonFieldMetaObjectHandler();
    }

    /**
     * @Author: JuiceYan
     * @Description: 返回map时，key 转驼峰
     * @param: []
     * @return: com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer
     * @throws:
     * @Date: 23:25 2018/12/5
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                configuration.setObjectWrapperFactory(new MapWrapperFactory());
            }
        };
    }
}
